package com.shyroke.daydayzhuanweb.controller;

import com.shyroke.daydayzhuanapi.constant.RedisKeyPrefix;
import com.shyroke.daydayzhuanapi.dto.LabelDto;
import com.shyroke.daydayzhuanapi.dto.LabelLabelListDto;
import com.shyroke.daydayzhuanapi.dto.TypeDto;
import com.shyroke.daydayzhuanapi.entity.LabelEntity;
import com.shyroke.daydayzhuanapi.entity.ProjectEntity;
import com.shyroke.daydayzhuanapi.entity.TypeEntity;
import com.shyroke.daydayzhuanapi.mapper.LabelMapper;
import com.shyroke.daydayzhuanapi.mapper.TypeMapper;
import com.shyroke.daydayzhuandao.respository.LabelRepository;
import com.shyroke.daydayzhuandao.respository.TypeRepository;
import com.shyroke.daydayzhuansvc.service.ILableService;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.R;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类名 ：
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2019/2/21 12:49
 */
@Controller
@RequestMapping(value = "admin/label")
public class LabelController {

    @Autowired
    private ILableService lableService;
    @Autowired
    private LabelRepository labelRepository;
    @Autowired
    private TypeRepository typeRepository;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LabelMapper labelMapper;
    @Autowired
    private TypeMapper typeMapper;

    /**
     * 跳转标签管理列表页面
     * @param model
     * @return
     */
    @RequestMapping(value = "listView")
    public ModelAndView listView(ModelAndView model,HttpSession session){
        model.setViewName("admin/label/list");
        return model;
    }

    /**
     * 获取表格的数据
     * @param page
     * @param label
     * @return
     */
    @RequestMapping(value = "getDataList")
    @ResponseBody
    public TabelData<LabelLabelListDto> getTableData(Page page, LabelDto label){
        return lableService.getTableData(page,label);
    }

    /**
     * 修改
     * @param isShowTitle
     * @return
     */
    @RequestMapping(value = "updateShowTitile")
    @ResponseBody
    public R updateShowTitile(String labelName,Integer isShowTitle){
        LabelEntity label = lableService.findByLableName(labelName);
        if(label == null){
            return R.error("没有该label");
        }
        label.setIsShowTitle(isShowTitle);
        labelRepository.save(label);
        return R.ok("操作成功");
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "updateTop")
    @ResponseBody
    public R updateTop(String labelName,Boolean isTop){
        LabelEntity label = lableService.findByLableName(labelName);
        if(label == null){
            return R.error("没有该label");
        }
        label.setIsTop(isTop);
        labelRepository.save(label);
        return R.ok("操作成功");
    }

    /**
     * 修改
     * @param label
     * @return
     */
    @RequestMapping(value = "update")
    @ResponseBody
    public R update(@RequestBody LabelDto label){
        LabelEntity labelObj = labelRepository.findByLabelName(label.getLabelName());
        if(labelObj == null){
            return R.error("没有该label");
        }
        labelObj.setKeys(label.getKeys());
        labelObj.setIsShowTitle(label.getIsShowTitle());
        if(label.getTypeId() == null) {
            labelObj.setType(null);
        }else {
            TypeEntity type = new TypeEntity();
            type.setId(label.getTypeId());
            labelObj.setType(type);
        }
        labelRepository.save(labelObj);
        return R.ok("操作成功");
    }

    /**
     * 删除标签
     * @param labelName
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public R delete(String labelName){
        LabelEntity label = labelRepository.findByLabelName(labelName);
        if(label == null){
            return R.error("没有该label");
        }
        labelRepository.delete(label);
        return R.ok("操作成功");
    }

    /**
     * 跳转新增页面
     * @param model
     * @return
     */
    @RequestMapping(value = "toAdd")
    public ModelAndView toAdd(ModelAndView model){
        model.setViewName("admin/label/add");
        return model;
    }

    /**
     * 删除标签
     * @param Label
     * @return
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public R save(@RequestBody LabelDto Label){
        lableService.saveOne(Label);
        return R.ok("操作成功");
    }

    /**
     * 跳转到编辑页面
     * @return
     */
    @RequestMapping(value = "/toEdit/{labelName}")
    public ModelAndView siteToEdit(ModelAndView model, @PathVariable("labelName")String labelName){
        model.addObject("label",labelMapper.entityToAdminDto(labelRepository.findByLabelName(labelName)));
        model.setViewName("admin/label/edit");
        return model;
    }

    /**
     * 获取所有标签和类型
     *
     * @return
     */
    @RequestMapping(value = "getLabelAndTypeAndSelected/{id}")
    @ResponseBody
    public R getLabelAndTypeAndSelected(@PathVariable(value = "id",required = false)Integer labelId) {

        if(labelId == null){
            return R.error("标签id不能为空！");
        }

        //获取该项目的标签以及类型
        Optional<LabelEntity> optionalProject = labelRepository.findById(labelId);
        if(!optionalProject.isPresent()){
            return R.error("id对应实体为空,id:"+labelId);
        }
        LabelEntity label = optionalProject.get();
        List<TypeDto> typeList = new ArrayList<>();
        Iterator<TypeEntity> iterator = typeRepository.findAll().iterator();
        while (iterator.hasNext()) {
            TypeEntity entity = iterator.next();
            typeList.add(typeMapper.entityToDto(entity));
        }
        List<LabelEntity> labelList = labelRepository.findByIsShowTitle(0);
        List<LabelDto> labelDtoList = labelList.stream().map(e -> labelMapper.entityToDto(e)).collect(Collectors.toList());
        //标记该项目已选择的标签和类型
        for(TypeDto t:typeList){
            if(label.getType() != null && label.getType().getId().intValue() == t.getId().intValue()){
                t.setSelected(true);
            }
        }

        Map<String, Object> labelTypeList = new HashMap<>();
        labelTypeList.put("typeList", typeList);

        return R.ok("labelTypeList", labelTypeList);
    }

}
